/* global document */
import React from 'react'
import PropTypes from 'prop-types'
import {AutoComplete, Button, Col, DatePicker, Form, Icon, Input, Row, Select} from 'antd'
import classnames from 'classnames'
import './Filter.less'
import {message} from "antd/lib/index"

const FormItem = Form.Item
const {Option} = Select

const ColProps = {
  xs: 6,
  sm: 6,
  style: {
    marginBottom: 6,
  },
}

const formItemLayout = {
  labelCol: {span: 8},
  wrapperCol: {span: 16},
  style: {
    marginBottom: 10,
  },
}

const status = []

const Filter = ({
                  expandForm,
                  filter,
                  onFilterChange,
                  empList,
                  departments,
                  productType,
                  orderType,
                  prePayType,
                  handleSearch,
                  toggleForm,
                  form: {
                    getFieldDecorator,
                    getFieldsValue,
                    setFieldsValue,
                  },
                }) => {

  const handleFields = (fields) => {
    const {registerStartTime, registerEndTime, inputStartTime, inputEndTime} = fields
    if (registerStartTime) {
      fields.registerStartTime = registerStartTime.format('YYYY-MM-DD HH:mm:ss')
    }
    if (registerEndTime) {
      fields.registerEndTime = registerEndTime.format('YYYY-MM-DD HH:mm:ss')
    }
    if (inputStartTime) {
      fields.inputStartTime = inputStartTime.format('YYYY-MM-DD HH:mm:ss')
    }
    if (inputEndTime) {
      fields.inputEndTime = inputEndTime.format('YYYY-MM-DD HH:mm:ss')
    }
    return fields
  }

  const handleFormReset = () => {
    const fields = getFieldsValue()
    for (let item in fields) {
      if ({}.hasOwnProperty.call(fields, item)) {
        if (fields[item] instanceof Array) {
          fields[item] = []
        } else {
          fields[item] = undefined
        }
      }
    }
    setFieldsValue(fields)
    handleSubmit()
  }

  const valid = fields => {
    const {type, keyword} = fields

    if (!type && keyword) {
      message.warn("请选择查询类型")
      return false
    }

    return true
  }

  const handleSubmit = () => {
    let fields = getFieldsValue()
    if (!valid(fields)) {
      return
    }
    fields = handleFields(fields)
    onFilterChange(fields)
  }

  const handleChange = (key, values) => {
    let fields = getFieldsValue()
    fields[key] = values
    fields = handleFields(fields)
    onFilterChange(fields)
  }
  // const {type, keyword} = filter

  const simpleFields = [
    (
      <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        <FormItem {...formItemLayout} label="订单编号">
          {getFieldDecorator('sn')(<Input placeholder="运营微信号"/>)}
        </FormItem>
      </Col>
    ),
    (
      <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        <FormItem {...formItemLayout} label="下单员工">
          {getFieldDecorator('belongEmpId')(<AutoComplete
            dataSource={empList.map(item => <Option key={item.id}>{item.name}</Option>)}
            style={{width: '100%'}}
            onSearch={handleSearch}
            placeholder="员工编号或名称"
          />)}
        </FormItem>
      </Col>
    ),
    (
      <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        <FormItem {...formItemLayout} label="订单状态">
          {getFieldDecorator('status')(
            <Select allowClear placeholder="请选择状态">
              <Option value={2}>{'待审核'}</Option>
              <Option value={3}>{'审核不通过'}</Option>
              <Option value={4}>{'审核通过待发货'}</Option>
              <Option value={5}>{'已发货'}</Option>
              <Option value={6}>{'已签收'}</Option>
              <Option value={7}>{'退回中'}</Option>
              <Option value={8}>{'退回签收'}</Option>
              <Option value={9}>{'已取消'}</Option>
            </Select>,
          )}
        </FormItem>
      </Col>
    ),
  ]

  const renderSimpleForm = () => (
    <Form layout="horizontal">
      <Row gutter={24}>
        {simpleFields}
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem label={''}>
            <Button className="margin-right" type="primary" htmlType="submit" onClick={handleSubmit}>查询</Button>
            <Button onClick={handleFormReset}>重置</Button>
            <a style={{marginLeft: 8}} onClick={toggleForm}>展开 <Icon type="down"/></a>
          </FormItem>
        </Col>
      </Row>
    </Form>
  )

  const renderAdvancedForm = () => (
    <Form layout="horizontal">
      <Row gutter={24}>
        {simpleFields}
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="归属微号">
            {getFieldDecorator('wxId')(<Input placeholder="员工工号或姓名"/>)}
          </FormItem>
        </Col>
      </Row>
      <Row gutter={24}>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="好友微号">
            {getFieldDecorator('customerWxId')(<Input placeholder="员工工号或姓名"/>)}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="归属部门">
            {getFieldDecorator('belongDeptId')(
              <Select allowClear placeholder="请选择部门">
                {departments.map(item => (<Option value={item.id}>{item.name}</Option>))}
              </Select>,
            )}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="收货人">
            {getFieldDecorator('receiver')(<Input placeholder="请输入"/>)}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="收货电话">
            {getFieldDecorator('mobile')(<Input placeholder="请输入"/>)}
          </FormItem>
        </Col>
      </Row>
      <Row gutter={24}>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="快递单号">
            {getFieldDecorator('expressNum')(<Input placeholder="请输入"/>)}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="获客渠道">
            {getFieldDecorator('customerId')(
              <Select allowClear placeholder="请选择">
                <Option value={1}>{'IP1'}</Option>
                <Option value={2}>{'IP2'}</Option>
                <Option value={3}>{'IP3'}</Option>
              </Select>,
            )}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="产品类型">
            {getFieldDecorator('productTypeId')(
              <Select allowClear placeholder="请选择产品类型">
                {productType.map(item => (<Option value={item.id}>{item.name}</Option>))}
              </Select>,
            )}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="订单类型">
            {getFieldDecorator('orderTypeId')(
              <Select allowClear placeholder="请选择订单类型">
                {orderType.map(item => (<Option value={item.id}>{item.name}</Option>))}
              </Select>,
            )}
          </FormItem>
        </Col>
      </Row>
      <Row gutter={24}>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="预付方式">
            {getFieldDecorator('prepayTypeId')(
              <Select allowClear placeholder="请选择预付方式">
                {prePayType.map(item => (<Option value={item.id}>{item.name}</Option>))}
              </Select>,
            )}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 9}} md={4} sm={24}>
          <FormItem labelCol={{span: 5}} wrapperCol={{span: 18}} className={'x-ant-row'} label="下单日期">
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('ctimeBeginDate')(<DatePicker style={{width: '100%'}} placeholder="开始时间"/>)}
              </FormItem>
            </Col>
            <Col span={2}>
              <span style={{display: 'inline-block', width: '100%', textAlign: 'center'}}>
                -
              </span>
            </Col>
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('ctimeEndDate')(<DatePicker style={{width: '100%'}} placeholder="结束时间"/>)}
              </FormItem>
            </Col>
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 9}} md={4} sm={24}>
          <FormItem labelCol={{span: 5}} wrapperCol={{span: 18}} className={'x-ant-row'} label="签收日期">
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('signBeginDate')(<DatePicker style={{width: '100%'}} placeholder="开始时间"/>)}
              </FormItem>
            </Col>
            <Col span={2}>
              <span style={{display: 'inline-block', width: '100%', textAlign: 'center'}}>
                -
              </span>
            </Col>
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('signEndDate')(<DatePicker style={{width: '100%'}} placeholder="结束时间"/>)}
              </FormItem>
            </Col>
          </FormItem>
        </Col>
      </Row>
      <Row gutter={24}>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem {...formItemLayout} label="商品名称">
            {getFieldDecorator('productName')(<Input placeholder="请输入"/>)}
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 9}} md={4} sm={24}>
          <FormItem labelCol={{span: 5}} wrapperCol={{span: 18}} className={'x-ant-row'} label="发货日期">
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('sendTimeBeginDate')(<DatePicker style={{width: '100%'}} placeholder="开始时间"/>)}
              </FormItem>
            </Col>
            <Col span={2}>
              <span style={{display: 'inline-block', width: '100%', textAlign: 'center'}}>
                -
              </span>
            </Col>
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('sendTimeEndDate')(<DatePicker style={{width: '100%'}} placeholder="结束时间"/>)}
              </FormItem>
            </Col>
          </FormItem>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <FormItem label="inline" {...formItemLayout} className={'x-ant-row'} label="订单金额">
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('minOrderMoney')(<Input placeholder="最小值"/>)}
              </FormItem>
            </Col>
            <Col span={2}>
              <span style={{display: 'inline-block', width: '100%', textAlign: 'center'}}>
                -
              </span>
            </Col>
            <Col span={11}>
              <FormItem>
                {getFieldDecorator('maxOrderMoney')(<Input placeholder="最大值"/>)}
              </FormItem>
            </Col>
          </FormItem>
        </Col>
      </Row>
      <Row gutter={24}>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
        </Col>
        <Col {...ColProps} xl={{span: 6}} md={4} sm={24}>
          <Button className="margin-right" style={{marginLeft: '10%'}} type="primary" onClick={handleSubmit}>查询</Button>
          <Button onClick={handleFormReset}>重置</Button>
          <a style={{marginLeft: 8}} onClick={toggleForm}>收起 <Icon type="up"/></a>
        </Col>
      </Row>
    </Form>
  )

  const renderForm = () => {
    return expandForm ? renderAdvancedForm() : renderSimpleForm()
  }

  return (
    <div>
      <Row gutter={24}>
        <Col {...ColProps} xl={{span: 1}} md={{span: 2}} sm={{span: 6}}>
        </Col>
        {status.map(item => (
          <Col {...ColProps} xl={{span: 4}} md={{span: 4}} sm={{span: 12}}>
            <div className={classnames(styles['flex-status-bar'])} style={{color: `${item.color}`}}>
              <Icon type={item.icon} style={{fontSize: 20}} theme="outlined"/>&nbsp;
              <span>{item.name}：<span style={{fontWeight: 'bold', fontSize: 18}}>{item.count}</span></span>
            </div>
          </Col>
        ))}
      </Row>
      {renderForm()}
    </div>
  )
}

Filter.propTypes = {
  onAdd: PropTypes.func,
  form: PropTypes.object,
  filter: PropTypes.object,
  onFilterChange: PropTypes.func,
}

export default Form.create()(Filter)
